package com.easy.tool.utils.str;

import com.easy.tool.anno.BeanDes;
import com.easy.tool.anno.MethodDes;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 高俊
 * @create 2022-04-2022/4/29-10:02
 */
@BeanDes(name = "文字匹配",des = "用户文字匹配和计算匹配百分比")
public class TxMatchUtils {


    private static List<StringBuilder> result = new ArrayList<>();

    private static Integer node = 0;
    private static Integer curIndex = 0;
    private static int temp = 0;

    private static void clear(){
        result.clear();
        node = 0;
        curIndex = 0;
        temp = 0;
    }

    @MethodDes(des = "文字匹配",explain = "对文字进行匹配",param = {"arg0 -> term：匹配字符串，建议按顺序","arg1 -> text：目标字符串",})
    public static Map<Object, Object> TxMatch(String term, String text){
        // 防止循环匹配出现问题，进入前先初始化
        clear();

        List<Integer> indexList = new ArrayList<>();
        StringBuilder src = new StringBuilder(term);
        StringBuilder target = new StringBuilder(text);

        for (int i = 0; i < src.length(); i++) {
            char c = src.charAt(i);
            // 从当前下标开始进行文字匹配
            int index = target.indexOf(String.valueOf(c),curIndex);
            // 将当前下标重新赋值为 匹配到的下标
            curIndex = target.indexOf(String.valueOf(c));
            indexList.add(index);
        }
        getWord(indexList,target);


        // 组装返回map
        Map<Object, Object> resultMap = new HashMap<>();
        resultMap.put("word",result);
        resultMap.put("match",caculation(text));
        return resultMap;
    }

    private static void getWord(List<Integer> indexList,StringBuilder text){
        StringBuilder builder = new StringBuilder();
        for (int i = 1; i < indexList.size(); i++) {
            // 判断与前一个是否连续
            Integer last = indexList.get(i - 1);
            Integer current = indexList.get(i);
            // 当 当前下标==上一个下标
            if (last == (current -1)){
                eqIndex(i,indexList,builder,last,current,text);
            }else {
                builder = neIndex(builder,last,text);
            }
        }
    }

    private static void eqIndex(int i,List<Integer> indexList,StringBuilder builder,Integer last,Integer current,StringBuilder text){
        // 下标 不是 最后一个，且与上一个相等
        if (i != (indexList.size()-1)){
            builder.append(text.charAt(last));
            temp++;
        }else {
            // 否则就会将当前和上一个插入
            builder.append(text.charAt(last));
            builder.append(text.charAt(current));
            result.add(builder);
        }
    }
    private static StringBuilder neIndex(StringBuilder builder,Integer last,StringBuilder text){
        if (temp > 0){
            builder.append(text.charAt(last));
            result.add(builder);
            builder = new StringBuilder();
            node++;
        }
        temp = 0;
        return builder;
    }

    // 计算匹配度
    private static float caculation(String text){
        // 计算匹配百分比
        float matching = 0;
        for (StringBuilder r : result) {
            matching += r.length();
        }
        return (matching / (float) (text.length()) * 100);
    }
}
